<?php
namespace Home\Controller;
use Think\Controller;
use sinacloud\sae\Storage;
class IndexController extends Controller
{
    private $model;

    public function __construct()
    {
        parent::__construct();
        $this->model = D("Home/User");
    }

    /*
     * 主页：默认登录页面
     */
    public function index()
    {
        $this->display("home:login");
        //    	$this->redirect("login");
    }

    /*测试*/
    public function test()
    {
        $this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px } a,a:hover{color:blue;}</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>！</p><br/>版本 V{$Think.version}</div><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_55e75dfae343f5a1"></thinkad><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>', 'utf-8');
    }

    /*登录*/
    public function login()
    {
        $res = $this->model->chkAccountAvai();
        if ($res) {
            return $this->ajaxReturn(array('status' => 1, msg => ""));
        } else {
            return $this->ajaxReturn(array('status' => 0, msg => "密码错误"));
        }
    }

    /*退出*/
    public function logout()
    {
        session('userName', '');
        session('userId', '');
        $this->display("home:login");
    }

    /*poster 推送列表页*/
    public function poster()
    {

        $this->redirect("posterList");
    }

    public function authCheck(){
        $sessName = session('userName');
        if (!isset($sessName)) {
            redirect("index");
            exit;
        }
    }
    /*poster 推送列表页*/
    public function posterList()
    {
        $this->authCheck();
        $data = $this->model->getAllPosters();
        $this->assign('data', $data);
        $this->display("home:table");
    }

    /*保存poster*/
    public function posterPersist()
    {
        $this->authCheck();
        $res = $this->model->savePoster();
        if (!$res) {
            $this->ajaxReturn(array('status' => 0, msg => "密码错误"));
        } else {
            $this->ajaxReturn(array('status' => 1, msg => "成功添加一条记录！"));
        }
    }

    /*编辑poster*/
    public function posterEdit()
    {
        $this->authCheck();
        $id = I("post_id");
        if (empty($id)) {
            /*新增推送*/
            $this->display('home/form');
            exit;
        }
        $data = $this->model->getOneById($id);
        $data['post_content'] = htmlspecialchars_decode($data['post_content']);
        $this->assign('data', $data);
        $this->display("home/form");
    }

    /*预览poster*/
    public function posterPreview()
    {
       
        $id = I("post_id");
        /*初始化微信jssdk*/
        $signPackage = initJSSKD();
        
        $data = $this->model->getOneById($id);
        $this->assign('data',$data);
        $this->assign('post_content', htmlspecialchars_decode($data['post_content']));
        $this->assign('signPackage', $signPackage);
        $this->display('home:poster');
    }

    /*删除poster*/
    public function posterDelete()
    {
        $this->authCheck();
        $idStr = I("idStr");
        $res = $this->model->deleteByIds($idStr);
        if ($res) {
            $this->ajaxReturn(array('status' => 1, msg => ""));
        } else {
            $this->ajaxReturn(array('status' => 0, msg => "删除出错"));
        }
    }

    /*新增推送*/
    public function newPoster()
    {
        $this->authCheck();
        $this->display('home/form');
    }

    /*图片管理列表*/
    public function galleryList()
    {
    	$this->authCheck();
		
    	$accessKey = "lanco:04mwy2x324";
		$SecretKey = "wx1yw205wyyk0220x4h4k1wyi4hmwl220mz403l2";
    	Storage::setAuth($accessKey, $SecretKey);
		$res  = Storage::getBucket('postimg', '', null, 1000, '/');
        //遍历storage 文件
		$parent = array();
	   	foreach ($res as $key=>$value) {
	   		$prefix = $value['subdir'];
			$index =preg_replace('/\//','',$prefix);
	   		$parent[$index]=array();
			$res_sub  = Storage::getBucket('postimg', $prefix, null, 1000, '/');
            foreach ($res_sub as $key=>$value) {
	            array_push($parent[$index],$value['name']);
	        }
        }
        
        $filesImgDir = Storage::getUrl('postimg');
        $this->assign('fileDir',$filesImgDir);
        $this->assign('fileList',$parent);
        $this->display("home/gallery");
    }
    /*删除图片*/
    public function deletePosterImg(){
        $this->authCheck();
        $srcImg = I("imgSrc");
        if(!empty($srcImg)){
            $s = new Storage();//实例化一个sae的stroage原生类。
			$suffix = preg_replace('/http:\/\/.*\-.*(\.com\/)/','',$srcImg);
            $result=$s->deleteObject('postimg',$suffix); //调用删除方法删除文件。
            If ($result == false) {
                $this->ajaxReturn(array('status'=>0,"msg"=>"删除文件失败"));
            } else {
                $this->ajaxReturn(array('status'=>1,"msg"=>""));
            }
        }
    }
}